<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isAdmin" label="是否是总代理">
          <a-switch v-model="isAdminFlag" @change="isAdminClick()" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status" label="状态">
          <a-switch v-model="statusFlag" @change="statusClick()" />
        </a-form-model-item>
<!--        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="royaltyStatus" label="提成开关">-->
<!--          <a-switch v-model="royaltyStatusFlag" @change="royaltyStatusClick()" />-->
<!--        </a-form-model-item>-->
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operationState" label="运营状态">
          <a-radio-group name="radioGroup" v-model="model.operationState">
            <a-radio value="0">暂停服务</a-radio>
            <a-radio value="1">运营中</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopType" label="服务商类型">
          <a-radio-group name="radioGroup" v-model="model.shopType">
            <a-radio value="0">直营</a-radio>
            <a-radio value="1">加盟</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serveRate" label="分红比例">
          <a-input-number v-model="model.serveRate" :min="0" :precision="2" :defaultValue="0" /> %
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="agencyName" label="服务商名称">
          <a-input placeholder="请输入服务商名称" v-model="model.agencyName" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="principalName" label="负责人姓名">
          <a-input placeholder="请输入负责人姓名" v-model="model.principalName" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="principalPhone" label="负责人手机号">
          <a-input placeholder="请输入负责人手机号" v-model="model.principalPhone" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="province" label="省">
          <a-select
            v-model="model.province"
            :label="model.a"
            style="width: 100%"
            :options="formatData(this.allList)"
            @change="cityClick"
            placeholder="请选择"
          />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="city" label="市">
          <a-select
            v-model="model.city"
            style="width: 100%"
            :options="formatData(this.citys)"
            @change="citysClicks"
            placeholder="请选择"
          />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="agencyServicePhone" label="客服电话">
          <a-input placeholder="请输入客服电话" v-model="model.agencyServicePhone" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="agencyServiceUrl" label="在线客服链接地址">
          <a-input placeholder="请输入客服电话" v-model="model.agencyServiceUrl" />
        </a-form-model-item>
        <!-- <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="topRate" label="提成比例">
          <a-input-number v-model="model.topRate" :min="0" :precision="2" :defaultValue="0" />
        </a-form-model-item> -->
        <!-- <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="balance" label="服务商提成">
          <a-input-number v-model="model.balance" :min="0" :precision="2" :defaultValue="0" />
        </a-form-model-item> -->
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idCard" label="身份证号">
          <a-input placeholder="请输入身份证号" v-model="model.idCard" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idCardImg" label="身份证照片">
          <j-image-upload class="avatar-uploader" text="上传身份证正面" v-model="model.idCardImg"></j-image-upload>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idCardImg" label="身份证照片">
          <j-image-upload class="avatar-uploader" text="上传身份证反面" v-model="model.idCardReverseImg"></j-image-upload>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessLicense" label="营业执照">
          <j-image-upload
            class="avatar-uploader"
            text="上传正面营业执照"
            v-model="model.businessLicense"
          ></j-image-upload>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import Area from '@/components/_util/Area'
import moment from 'moment'
// 电话号码校验
let principalPhoneJ = async (rule, value) => {
  if (new RegExp(/^1[0-9]{10}$/).test(value)) {
    return Promise.resolve()
  } else {
    return Promise.reject('您的手机号码格式不正确!')
  }
}
// 身份证校验
let validateIdCard = async (rule, value) => {
  if (new RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/).test(value)) {
    return Promise.resolve()
  } else {
    return Promise.reject('您的身份证号码格式不正确!')
  }
}
export default {
  name: 'XhdjAgencyModal',
  data() {
    return {
      title: '操作',
      visible: false,
      previewVisible: false,
      isAdminFlag: false,
      statusFlag: false,
      royaltyStatusFlag: false,
      model: { shopType: 0 },
      allList: [], // 省份数据
      citys: [],
      cityss: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        principalName: [{ required: true, message: '请填写负责人', trigger: 'change' }],
        principalPhone: [{ required: true, validator: principalPhoneJ, trigger: 'blur' }],
        idCard: [{ required: true, validator: validateIdCard, trigger: 'blur' }],
        idCardImg: [{ required: true, message: '请上传身份证背面', trigger: 'change' }],
        idCardReverseImg: [{ required: true, message: '请上传身份证反面', trigger: 'change' }],
        businessLicense: [{ required: true, message: '请上传营业执照', trigger: 'change' }],
        operationState: [{ required: true, message: '请选择运营状态', trigger: 'change' }],
      },
      fileList: [
        {
          uid: '-1',
          name: 'image.png',
          status: 'done',
          url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
        },
      ],
      url: {
        add: '/xhdjAgency/add',
        edit: '/xhdjAgency/edit',
      },
    }
  },
  created() {
    this.all = new Area().all
    this.all.push({
      id: "330782",
      index:2,
      pid: "330000",
      text: "义乌市"
    })

    this.allList = this.all.filter((res) => res.index == 1)

  },
  methods: {
    add() {
      //初始化默认值
      this.edit({})
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.citys = this.all.filter((res) => res.pid == this.model.province)
      this.cityss = this.all.filter((res) => res.pid == this.model.city)
      this.isAdminFlag = this.isAdminAndstatus(this.model.isAdmin)
      this.statusFlag = this.isAdminAndstatus(this.model.status)
      this.royaltyStatusFlag = this.isAdminAndstatus(this.model.royaltyStatus)
      if (typeof this.model.operationState == 'number') {
        this.model.operationState = this.model.operationState.toString()
      }
      if (typeof this.model.shopType == 'number') {
        this.model.shopType = this.model.shopType.toString()
      }
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.isAdminFlag = false
      this.statusFlag = false
      this.royaltyStatusFlag = false
      this.$refs.form.clearValidate()
    },
    // 身份上传
    handleCancels() {
      this.previewVisible = false
    },
    async handlePreview(file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj)
      }
      this.previewImage = file.url || file.preview
      this.previewVisible = true
    },
    handleChange({ fileList }) {
      this.fileList = fileList
    },
    handleOk() {
      const that = this
      if (that.model.isAdmin != 1) {
        that.model.isAdmin = 0
      }
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    // 是否代理
    isAdminClick() {
      if (this.isAdminFlag) {
        this.model.isAdmin = 1
      } else {
        this.model.isAdmin = 0
      }
    },
    // 状态
    statusClick() {
      if (this.statusFlag) {
        this.model.status = 1
      } else {
        this.model.status = 0
      }
    },
    // 状态
    royaltyStatusClick() {
      if (this.royaltyStatusFlag) {
        this.model.royaltyStatus = 1
      } else {
        this.model.royaltyStatus = 0
      }
    },
    // 编辑代理和状态
    isAdminAndstatus(value) {
      if (value) {
        return true
      } else {
        return false
      }
    },
    // 下拉框格式转换
    formatData(array) {
      var arr = []
      arr = array.forEach((res) => {
        res.value = res.id
        res.label = res.text
      })
      return array
    },
    cityClick(value, text) {

      this.model.area = null
      this.model.city = null
      this.model.cityName = null
      this.model.provinceName = text.data.props.text
      this.citys = this.all.filter((res) => res.pid == this.model.province)

    },
    citysClicks(value, text) {
      this.model.cityName = text.data.props.text
      this.cityss = this.all.filter((res) => res.pid == this.model.city)
      this.$forceUpdate()
    },
  },
}
</script>

<style lang="less" scoped>
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
